<?php
declare (strict_types = 1);

namespace app\command;

use app\service\BaiduKeywordsSpotting;
use think\console\Command;
use think\console\Input;
use think\console\Output;


class RunData extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('rundata')
            ->setDescription('the rundata command');
    }

    protected function execute(Input $input, Output $output){
        $this->get_data();
    }

    public function get_data(){
        $spider = new BaiduKeywordsSpotting();
        $list = \think\facade\Db::name('keywords_spotting')->select();
        for ($i = 0;$i < 10;$i++) {
            foreach ($list AS $key => $value) {
                $results[$value['keyword']][] = $spider->search($value['keyword'], 1); // 搜索“安全阀”，第1页
                sleep(2);
                $results[$value['keyword']][] = $spider->search($value['keyword'], 2);
            }
            sleep(3);
        }
        $email = \think\facade\Db::name('keywords_email')->select();
        $keyword = \think\facade\Db::name('keywords_spotting')->select();
        $arr = [];
        foreach ($keyword AS $key => $value)
            $arr[$value['keyword']][] = $value['corporate_name'];
        $string = '';
        foreach ($results AS $key => $value){
            if (empty($value)){
                $string .= '<tr><td>关键词:' . $key . '</td><td>找不到任何相关推广广告</td><td></td></tr>';
                continue;
            }
            foreach ($value AS $index => $item){
                if (empty($item))continue;
                $msg = $arr[$item['关键词']];
                foreach ($msg AS $keys => $values){
                    if (strpos($item['企业'],$values) !== false){
                        unset($arr[$item['关键词']][$keys]);
                        $string .= '<td>关键词:'.$item['关键词'].'</td><td>公司:'.$values.'排名:'. $item['位置'] .'</td>';
                    }
                }
            }
            var_dump($value);
        }
        foreach ($arr AS $key => $value){
            if (empty($value))continue;
            foreach ($value AS $index => $item)
                $string .= '<tr><td>关键词:' . $key . '</td><td>企业:'. $item .'找不到推广广告</td></tr>';
        }
        if (!empty($string)) {
            $body = file_get_contents(public_path() . '/../view/index/email.html');
            $head = '<tr><th>关键词</th><th>提示</th></tr>';
            $body = str_replace('{head}', $head, $body);
            $body = str_replace('{content}', $string, $body);
            foreach ($email AS $key => $value){
                $this->send($value['email'], '百度监测关键词异常结果显示', $body);
            }
        }
        die('ko');
    }

    private function send($email,$title = '',$body = ''){
        $message = new \Swift_Message($title);
        $message = $message->setFrom('ppf1788@sina.com');
        $message = $message->setTo($email);
        $message = $message->setBody($body,'text/html' );
        $transport = new \Swift_SmtpTransport('smtp.sina.com', 465,'ssl');
        $transport = $transport->setUsername('ppf1788@sina.com');
        $transport = $transport->setPassword('14c1f2883d0b344e');
        $mailer = new \Swift_Mailer($transport);
        $mailer->send($message);
        //return json(['code' => 200,'msg' => '成功','data' => $res],200);
    }
}
